import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';

/**
 * UIAbility 相当于我们应用中的一个任务，我们可以把自己的App想象成一个UIAbility，但是当项目越来越大，需要拓展和分担业务的时候，就可以采用多个Ability了。
 * Ability声明周期：
 * onCreate > onWindowStageCreate > onForeground > onBackground > onWindowStageDestroy > onDestroy
 * 直接过滤生命周期
 05-19 16:30:04.183 12111-15146/? I 00000/生命周期: onCreate111 2
 05-19 16:30:04.195 12111-15146/? I 00000/生命周期: Ability onWindowStageCreate
 05-19 16:30:04.288 12111-15146/? I 00000/生命周期: Ability onForeground
 05-19 16:30:04.330 12111-15146/? I 00000/生命周期: Succeeded in loading the content. Data:
 05-19 16:30:04.334 12111-15146/? I 0FEFE/JsApp: 生命周期----- aboutToAppear
 05-19 16:30:04.361 12111-15146/? I 0FEFE/JsApp: 生命周期----- onPageShow
 // 点击下一页
 05-19 16:30:12.873 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 下一页----- aboutToAppear Next
 05-19 16:30:12.874 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageHide
 05-19 16:30:12.874 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 下一页----- onPageShow
 // 系统返回侧滑
 05-19 16:30:25.241 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 下一页----- onBackPress
 05-19 16:30:25.242 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 下一页----- onPageHide
 05-19 16:30:25.242 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageShow
 05-19 16:30:25.561 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 下一页----- aboutToDisappear
 // 息屏
 05-19 16:30:34.906 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onBackground
 05-19 16:30:34.918 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageHide
 // 亮屏
 05-19 16:30:43.061 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onForeground
 05-19 16:30:43.067 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageShow
 // home
 05-19 16:30:49.315 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onBackground
 05-19 16:30:49.347 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageHide
 // 重新打开(热打开)
 05-19 16:30:57.812 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onForeground
 05-19 16:30:57.813 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onPageShow
 // 系统返回-侧滑
 05-19 16:31:01.229 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- onBackPress
 05-19 16:31:01.254 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onBackground
 05-19 16:31:01.261 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- aboutToDisappear // 一个nav
 05-19 16:31:01.261 12111-15146/com.walker.hmfirst I 0FEFE/JsApp: 生命周期----- aboutToDisappear // 一个page
 05-19 16:31:01.268 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onWindowStageDestroy
 05-19 16:31:01.268 12111-15146/com.walker.hmfirst I 00000/生命周期: Ability onDestroy
 *
 *
 * 跳转
 * Stage(组织、阶段) 模型。现在的主流模式，FA是鸿蒙老版本模型
 * 模块  【一个项目可能有多个模块
 * UIAbility 项目默认有一个-任务窗口-绘制页面 【一个模块可能有多个page
 * Page 【一个页面可能有多个组件
 * component 【组件-UI库
 * > UI Ability组件是系统调度的基本单元，为应用提供绘制界面的窗口；
 * > 一个Ability中可以通过多个Page了实现一个功能模块
 * > 每个Ability实例，都对应一个最近任务列表中的任务
 * 当我们项目中要拆解多个任务的时候，可以通过新建多个Ability的方法是进行任务的拆解
 * 这时就涉及Ability之间的跳转交互了
 * >
 */
export default class RouterDemoAbility extends UIAbility {
  logInfo: string = '[ "粤菜", "海鲜" ], "price": 88.8, "picture": "https://img0.baidu.com/it/u=1893945504,3019536334&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1715446800&t=e6f2bea05582e4ff75cd0e158e1622d0", "description": "鱼肉鲜嫩，汤汁清澈，原汁原味。", "tag": "特价菜", "count": 0 }, { "id": 4, "name": "白切鸡", "like_ratio_desc": "92%好评", "month_saled": 600, "unit": "只", "food_tag_list": [ "粤菜", "鸡肉" ], "price": 58.8, "picture": "https://img1.baidu.com/it/u=4056647523,2958516124&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1715446800&t=9cea22911540eaba6568ef47686a4b41", "description": "皮爽肉滑，清淡鲜美，佐以姜葱。", "tag": "经典粤菜", "count": 0 } ] } ] }'

  // 新建
  onCreate(want, launchParam) {
    // hilog HM日志的方法。
    // hilog.info(0x0000, 'onCreate', '%{public}s', 'Ability onCreate', this.logInfo);// 系统默认
    // hilog.info(0x0000, '生命周期', 'onCreate111');
    hilog.info(0x0000, '生命周期', 'onCreate111 2', this.logInfo);
    // console.log('生命周期', 'onCreate111 3', this.logInfo); //console 通常用于页面内的日志打印
    // logInfo 太长都打印，短了后仅3打印 可以直接过滤onCreate111
    //05-19 16:14:42.094 3482-14088/? I 00000/RouterDemoAbility: onCreate111
    // 05-19 16:14:42.094 3482-14088/? I 00000/RouterDemoAbility: onCreate111 2
    // 05-19 16:14:42.094 3482-14088/? I 0FEFE/JsApp: RouterDemoAbility onCreate111 3 [ "粤菜", "海鲜" ], "price": 88.8, "picture": "https://img0.baidu.com/it/u=1893945504,3019536334&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1715446800&t=e6f2bea05582e4ff75cd0e158e1622d0", "description": "鱼肉鲜嫩，汤汁清澈，原汁原味。", "tag": "特价菜", "count": 0 }, { "id": 4, "name": "白切鸡", "like_ratio_desc": "92%好评", "month_saled": 600, "unit": "只", "food_tag_list": [ "粤菜", "鸡肉" ], "price": 58.8, "picture": "https://img1.baidu.com/it/u=4056647523,2958516124&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1715446800&t=9cea22911540eaba6568ef47686a4b41", "description": "皮爽肉滑，清淡鲜美，佐以姜葱。", "tag": "经典粤菜", "count": 0 } ] } ] }

  }

  // 销毁
  onDestroy() {
    hilog.info(0x0000, '生命周期', '%{public}s', 'Ability onDestroy');
  }

  // 窗口创建
  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, '生命周期', '%{public}s', 'Ability onWindowStageCreate');

    // Ability默认加载的页面
    windowStage.loadContent('pages/lifecycle/PageLifecycle', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, '生命周期', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, '生命周期', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  // 窗口销毁
  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, '生命周期', '%{public}s', 'Ability onWindowStageDestroy');
  }

  // 进入前台(可视)
  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, '生命周期', '%{public}s', 'Ability onForeground');
  }

  // 进入后台
  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, '生命周期', '%{public}s', 'Ability onBackground');
  }
}
